package com.longcai.piaojia.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.activity.NewsDetailActivity;
import com.longcai.piaojia.adapter.NewsAdapter;
import com.longcai.piaojia.adapter.NewsRecyclerAdapter;
import com.longcai.piaojia.adapter.RushedAdapter2;
import com.longcai.piaojia.conn.ShowTypeListGet;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/8/2.
 */
public class NewsFragment extends BaseFragment {


    @BoundView(R.id.news_lv) private PullToRefreshListView pullToRefreshListView;
    @BoundView(R.id.news_type_rv) private RecyclerView news_type_rv;
    private ListView news_lv;

    private List<String> lists = new ArrayList<>();
    private NewsAdapter adapter;

    private NewsRecyclerAdapter recyclerAdapter;
    private List<String> news_type_lists = new ArrayList<>();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = BoundViewHelper.boundView(this, inflater.inflate(R.layout.fragment_life, null));


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        news_type_rv.setLayoutManager(linearLayoutManager);

        recyclerAdapter = new NewsRecyclerAdapter(getActivity(), news_type_lists);
        news_type_rv.setAdapter(recyclerAdapter);

        recyclerAdapter.setOnItemClickListener(new NewsRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int pos) {

                recyclerAdapter.notifyDataSetChanged(news_type_lists, pos);

            }
        });

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {

                for (int i = 0; i < 10; i++) {

                    news_type_lists.add("全部分类");
                }

                recyclerAdapter.notifyDataSetChanged(news_type_lists, 0);
            }
        });



        pullToRefreshListView.setScrollLoadEnabled(false);
        pullToRefreshListView.setPullLoadEnabled(false);
        pullToRefreshListView.setPullRefreshEnabled(true);

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                pullToRefreshListView.onPullUpRefreshComplete();
                pullToRefreshListView.onPullDownRefreshComplete();

                setLastUpdateTime();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });


        news_lv = pullToRefreshListView.getRefreshableView();

        news_lv.setDivider(ContextCompat.getDrawable(getContext(), R.color.background_bg_color));

        news_lv.setDividerHeight(ScaleScreenHelperFactory.getInstance().getWidthHeight(18));

        news_lv.setVerticalScrollBarEnabled(false);

        for (int i = 0; i < 15; i++) {

            lists.add("");
        }

        news_lv.setAdapter(adapter = new NewsAdapter(getActivity(), lists));

        news_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                startActivity(new Intent(getContext(), NewsDetailActivity.class));

            }
        });



        return view;
        
    }

    private void setLastUpdateTime() {

        String text = new SimpleDateFormat("MM-dd HH:mm").format(new Date(System.currentTimeMillis()));

        pullToRefreshListView.setLastUpdatedLabel(text);
    }

}
